/**
 * 
 */
package gui.customControl;

import gui.util.IconFactory;

import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;

import data.Data.BlockActionType;
import data.Data.TeamID;

/**
 * Action dialog for solving the Block result after a dice roll.
 * 
 * @author Bogdan
 */
public class BlockActionDialog extends JDialog {
	private static final long serialVersionUID = -3374046072315981363L;

	public BlockActionDialog(final JFrame parent, final TeamID choosingTeam, final List<BlockActionType> blockActions) {
		super(parent, "The coach of " + choosingTeam + " choses the outcome", Dialog.ModalityType.DOCUMENT_MODAL);
		initializeComponets(blockActions);
		setDefaultCloseOperation(HIDE_ON_CLOSE);

		pack();
		setVisible(false);
	}

	private void initializeComponets(final List<BlockActionType> blockActions) {
		ActionListener listener = new ActionListener() {
			@Override
			public void actionPerformed(final ActionEvent e) {
				DiceFaceButton source = (DiceFaceButton) e.getSource();
				m_action = source.getBlockAction();

				dispose();
			}
		};

		JPanel panel = (JPanel) getContentPane();
		panel.setLayout(new FlowLayout());

		// initialize the buttons
		for (BlockActionType ba : blockActions) {
			DiceFaceButton btn = new DiceFaceButton(ba);
			btn.addActionListener(listener);

			panel.add(btn);
		}
	}

	/**
	 * Gets the selected block action.
	 * 
	 * @return
	 */
	public BlockActionType getAction() {
		setVisible(true);
		return m_action;
	}

	/**
	 * Represents the face of a block dice.
	 * 
	 * @author Bogdan
	 */
	private class DiceFaceButton extends JButton {
		private static final long serialVersionUID = 887309245795014937L;

		/** Constructor */
		public DiceFaceButton(final BlockActionType action) {
			super();
			this.m_action = action;
			setTooltip();
			setIcon(IconFactory.instance().getBlockDiceIcon(m_action));
		}

		public BlockActionType getBlockAction() {
			return this.m_action;
		}

		private void setTooltip() {
			switch (m_action) {
			case ATTACKER_DOWN:
				setToolTipText("Attacker down");
				break;
			case BOTH_DOWN:
				setToolTipText("Both down");
				break;
			case DEFENDER_DOWN:
				setToolTipText("Defender down");
				break;
			case DEFENDER_STUMBLES:
				setToolTipText("Defender stumbles");
				break;
			case PUSHED:
				setToolTipText("Pushed");
				break;
			}
		}

		private BlockActionType m_action;
	}

	private BlockActionType m_action = BlockActionType.PUSHED;
}
